package com.huawei.updatesdk.a.a.c.b;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.huawei.updatesdk.a.a.c.b.a.b;
import java.util.Locale;

public class a {
    private static final Uri a = Uri.parse("content://telephony/carriers/preferapn");
    private static final Uri b = Uri.parse("content://telephony/carriers/preferapn/0");
    private static final Uri c = Uri.parse("content://telephony/carriers/preferapn/1");

    public static class a {
        private String a;
        private String b;
        private String c;

        public String a() {
            return this.a;
        }

        public void a(String str) {
            this.a = str;
        }

        public String b() {
            return this.b;
        }

        public void b(String str) {
            this.b = str;
        }

        public void c(String str) {
            this.c = str;
        }
    }

    public static a a(Context context) throws SecurityException {
        Throwable e;
        Cursor cursor;
        Throwable th;
        Cursor cursor2;
        Throwable th2;
        a aVar = new a();
        Cursor d;
        try {
            d = b.b() ? d(context) : c(context);
            if (d != null) {
                while (d.moveToNext()) {
                    try {
                        aVar.a(d.getString(d.getColumnIndex("_id")));
                        aVar.c(d.getString(d.getColumnIndex("name")));
                        aVar.b(d.getString(d.getColumnIndex("apn")).toLowerCase(Locale.getDefault()));
                    } catch (SecurityException e2) {
                        e = e2;
                        cursor = d;
                    } catch (Throwable e3) {
                        th = e3;
                        cursor2 = d;
                        th2 = th;
                    } catch (Throwable th3) {
                        e = th3;
                    }
                }
                if (d != null) {
                    d.close();
                }
                return aVar.a() == null ? aVar : null;
            } else if (d == null) {
                return null;
            } else {
                d.close();
                return null;
            }
        } catch (Throwable e4) {
            th = e4;
            Object obj = null;
            e = th;
            try {
                com.huawei.updatesdk.a.a.b.a.a.a.a("ApnUtil", "getDefaultAPN, SecurityException: ", e);
                throw e;
            } catch (Throwable th4) {
                e = th4;
                d = cursor;
                if (d != null) {
                    d.close();
                }
                throw e;
            }
        } catch (Exception e5) {
            th2 = e5;
            cursor2 = null;
            try {
                com.huawei.updatesdk.a.a.b.a.a.a.a("ApnUtil", "getDefaultAPN, Exception: ", th2);
                if (cursor2 != null) {
                    cursor2.close();
                }
                if (aVar.a() == null) {
                }
            } catch (Throwable th5) {
                e = th5;
                d = cursor2;
                if (d != null) {
                    d.close();
                }
                throw e;
            }
        } catch (Throwable e42) {
            d = null;
            e = e42;
            if (d != null) {
                d.close();
            }
            throw e;
        }
    }

    public static boolean b(Context context) {
        try {
            a a = a(context);
            return a == null || (a.b() != null && a.b().contains("wap"));
        } catch (Throwable e) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("ApnUtil", "isWap(), SecurityException: ", e);
            return false;
        }
    }

    private static Cursor c(Context context) {
        return context.getContentResolver().query(a, null, null, null, null);
    }

    private static Cursor d(Context context) {
        Cursor f;
        if (b.a().a() == 0) {
            f = f(context);
            if (f == null) {
                f = e(context);
            }
        } else {
            f = e(context);
            if (f == null) {
                f = f(context);
            }
        }
        return f == null ? c(context) : f;
    }

    private static Cursor e(Context context) {
        return context.getContentResolver().query(c, null, null, null, null);
    }

    private static Cursor f(Context context) {
        return context.getContentResolver().query(b, null, null, null, null);
    }
}
